﻿//点击搜索月嫂页面
function sousuo() {
    $('html').addClass('sift-back');
    setTimeout(function () {
        $('html').attr('class', '');
        $('.scrPop,.scrMenu').hide();
    }, 200);
    if ($("#beginTime").val() != "" && $("#datenumber").val() != "") {
        $(".scrResults b").html($("#beginTime").val() + "<span>预产并服务</span>" + $("#datenumber").val() + "天");
        $("#OrderStart").val($("#beginTime").val());
        ValueChange();
        InitCommentList("search");
    }
    else {
        InitCommentList("search");
    }
}
function ValueChange() {
    var days = $("#datenumber").val();
    var startdate1 = $("#beginTime").val();
    var d = new Date(startdate1);
    d.setDate(d.getDate() + parseInt(days));
    var m = d.getMonth() + 1;
    $("#OrderEnd").val(d.getFullYear() + '-' + m + '-' + d.getDate());
}
//排序问题处理
function paixu(ziduan) {
    $("#Orderby").val(ziduan);
    InitCommentList("search");
    $('.scrNav ul li a').removeClass("active");
    switch (ziduan) {
        case 'PInfoLastdate': $("#PInfoLastdate").addClass("active"); break;
        case 'PInfoAge': $("#PInfoAge1").addClass("active"); break;
        case 'PInfoLevel': $("#PInfoLevel").addClass("active"); break;
        case 'PInfoPrice': $("#PInfoPrice1").addClass("active"); break;
    }
}
//星星等级格式处理
function level(lvalue) {
    var xingji = "";
    switch (lvalue) {
        case 1: xingji = "★"; break;
        case 2: xingji = "★★"; break;
        case 3: xingji = "★★★"; break;
        case 4: xingji = "★★★★"; break;
        case 5: xingji = "★★★★★"; break;
        case 6: xingji = "★★★★★★"; break;
        case 7: xingji = "★★★★★★★"; break;
        case 8: xingji = "★★★★★★★★"; break;
    }
    return xingji;
}
//初始化月嫂列表
function InitCommentList(isSearch) {
    var search = init_search();
    var paixuValue = $("#Orderby").val();
    var OrderStart = $("#OrderStart").val();
    var OrderEnd = $("#OrderEnd").val();
    CommentAjaxList('#btnLoadComment', '#comment_list', 3, 500, '/api/PregnanterInfoApi/GetBootGridPregnanterInfos?format=json', 'sort[' + paixuValue + ']=desc&searchType=2&searchPhrase=' + search + '&OrderStart=' + OrderStart + '&OrderEnd=' + OrderEnd + '', isSearch);
}
//页面加载完毕事件
$(document).ready(function () {
    $('#beginTime').date();
    //初始化月嫂列表
    InitCommentList(""); //加载第一页评论列表
});
/*AJAX加载评论列表*/
var comment_page_index = 1; //评论页码全局变量
function CommentAjaxList(btnObj, listDiv, pageSize, pageCount, sendUrl, data, isSearch) {
    if (isSearch == "search") {
        comment_page_index = 1;
        $("#comment_list").html("");
    }
    //计算总页数
    var pageTotal = 0;
    //发送AJAX请求
    $.ajax({
        type: "post",
        url: sendUrl,
        dataType: "json",
        data: data + '&current=' + comment_page_index + '&rowCount=' + pageSize,
        beforeSend: function () {
            $(btnObj).prop("disabled", true);
            $(btnObj).val('正在加载...');
        },
        success: function (data) {
            var strHtml = '';
            pageTotal = data.total;
            $("#PageCount").html(pageTotal);
            for (var i in data.rows) {
                strHtml += '<li><section class="detailsInfo pd30"><section class="userImg fl">';
                strHtml += '<a href="/Yuesao/PregnanterInfo/MobileView?id=' + data.rows[i].Id + '" title="' + data.rows[i].PInfoName + '"><img src="' + data.rows[i].PInfoPhoto + '"></a>';
                strHtml += '<section class="validation clearfix"><em></em><span class="fl">雪绒花认证</span></section></section>';
                strHtml += '<section class="userInfo fl">';
                strHtml += '<p class="clearfix"><a class="name fl" href="/Yuesao/PregnanterInfo/MobileView?id=' + data.rows[i].Id + '" title="' + data.rows[i].PInfoName + '">' + data.rows[i].PInfoName + '</a><span class="ex-info fl">' + data.rows[i].PInfoCode + '</span></p>';
                strHtml += '<p>' + data.rows[i].PInfoAge + '岁，' + data.rows[i].PInfoPermanent + '人, ' + data.rows[i].PInfoEdage + '年经验</p>';
                strHtml += '<p><span class="gray9">星座属相：</span>' + data.rows[i].PInfoXingzuo + ' ' + data.rows[i].PInfoShuxiang + '</p>';
                strHtml += '<p><span class="gray9">月嫂等级：</span><span class="xing">' + level(data.rows[i].PInfoLevel) + '</span></p>';
                strHtml += '<p class="price">¥' + data.rows[i].PInfoPrice + '<span class="gray">/24时</span><a href="javascript:do_order('+ data.rows[i].Id +','+"'"+data.rows[i].PInfoName+"'"+','+"'"+data.rows[i].PInfoCode+"'"+');" title="预定">我要预定</a></p></section></section></li>';                             
            }
            //添加到列表
            if (comment_page_index == 1) {
                $(listDiv).html(strHtml);
            } else {
                $(listDiv).append(strHtml);
            }
            comment_page_index++; //当前页码加一
        },
        complete: function () {
            $(btnObj).prop("disabled", false);
            $(btnObj).val('加载更多月嫂');
        }
    });
}
//构造搜索条件
function init_search() {
    var searchParams = new Array();
    var fields = "PInfoAge,PInfoAge,PInfoPrice,PInfoPrice,PInfoClass,PInfoEdage,PInfoXingzuo,PInfoShuxiang,PInfoPriceType";
    searchParams.push(fields);
    var eqs = ">=,<=,>=,<=,=,=,=,=,like";
    searchParams.push(eqs);
    var age_start = "";
    var age_end = "";
    var price_start = "";
    var price_end = "";
    switch ($("#PInfoAge").val()) {
        case '1': age_start = 21; age_end = 40; break;
        case '2': age_start = 41; age_end = 60; break;
        case '3': age_start = 61; age_end = 80; break;
    }
    switch ($("#PInfoPrice").val()) {
        case '1': price_start = 3001; price_end = 5000; break;
        case '2': price_start = 5001; price_end = 7000; break;
        case '3': price_start = 7001; price_end = 9000; break;
        case '4': price_start = 9001; price_end = 1000000; break;
    }
    var tj_class = encodeURI($.trim($("#PInfoClass").val()));
    var ed_age = $.trim($("#PInfoEdAge").val());
    var xz = encodeURI($.trim($("#PInfoXingzuo").val()));
    var sx = encodeURI($.trim($("#PInfoShuxiang").val()));
    var pricetype = encodeURI($.trim($("#PInfoPriceType").val()));
    var values = age_start + "," + age_end + "," + price_start + "," + price_end + "," + tj_class + "," + ed_age + "," + xz + "," + sx + "," + pricetype;
    searchParams.push(values);
    var ops = "AND,AND,AND,AND,AND,AND,AND,AND,AND";
    searchParams.push(ops);
    var searchPhrase = searchParams.join("|");
    return searchPhrase;
}
//预定
function do_order(id, name, code){
     var flag = true;
        $("#yuesaoes div").each(function () {
            var hid = $(this).find("input[type='hidden']").val();
            //alert(hid);
            if (id == hid) {
                alert("您已经选择此月嫂，请勿重复添加！");
                flag = false;
            }
        });
        if (flag) {
            var ys = $("#yuesaoes").find(".selectlist");
            var has_count = ys.length;
            if (has_count >= 3) {
                alert("对不起，最多只能预定3个月嫂！");
            } else {//预定
                var ordhtml = '<div class="selectlist">' + name + ' <span onclick="del_yuesao(this);">×</span><input type="hidden" name="YuesaoId" value="' + id + '"/></div>';
                $("#yuesaoes").append(ordhtml);
                showTopMsg("<div>恭喜您，预定成功！您当前已预订" + (parseInt(has_count) + 1) + "个月嫂，还可以预定" + (3 - (parseInt(has_count) + 1)) + "个。</div>", "温馨提示", 400, 95);
            }
        }
}

		//得到对应月嫂集
function get_selected_yuesaos() {
            var ids = "";
            var rowIds = [];
            $("#yuesaoes div").each(function () {
                var hid = $(this).find("input[type='hidden']").val();
                rowIds.push(hid);
            });
            ids = rowIds.join(",");
            return ids;
        }

		//删除已选择的月嫂
        function del_yuesao(obj) {
            var ys = $(obj).parent(".selectlist");
            $(ys).hide("slow").remove();
        }

        function step3() {   
			if($("#OrderStart").val()==""||$("#OrderEnd").val()=="")       
         {
			 alert("请先选择对应预定时间及服务天数！");
			 return false;
		 }
            var ys = $("#yuesaoes").find(".selectlist");
            var ids = get_selected_yuesaos();
            if (ys.length <= 0) {
                alert("月嫂至少需要选择一个！");
                return false;
            }
            var step4_url = '/Order/YuesaoOrder/MobileAddYuesaoInfo/?ids=' + ids + "&order_start=" + $("#OrderStart").val() + "&order_end=" + $("#OrderEnd").val() + "&price_type=" + $("#PInfoPriceType").val();
            window.location.href = step4_url;
        }